// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geniusai.aip.auth.domain.DataSpace;
import com.geniusai.aip.auth.domain.UserDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * @author hml14563
 * @date 2021/3/18
 */
public interface UserMapper extends BaseMapper<UserDO> {
    @Select("select sum(volume_size) from password")
    Long sumTotalAllocatedVolumeSize();

    @Select("select sum(vgpu_memory) from password")
    Integer sumVGpuMemory();


    @Select({
            "<script>",
            "select * from aip.data_space where data_space_name in ",
            "<foreach collection='userIds' item='userId' open='(' separator=',' close=')'>",
            "#{userId,jdbcType=VARCHAR}",
            "</foreach>",
            "</script>"})
    List<DataSpace> selectDataSpaceByNames(@Param("userIds") Set<String> userIds);

    @Select("SELECT count(1) FROM aip.job_execute_record jer " +
            "JOIN aip.job j ON jer.job_id = j.id " +
            "WHERE j.owner_name =#{userId,jdbcType=VARCHAR} " +
            "AND jer.status IN ('queue','scheduled','running')")
    Integer countRunningJobsByUserId(@Param("userId") String userId);

}
